package main

import (
	"fmt"
	"math/rand"
	"sort"
)

type A interface {
	Test1()
}

type AS struct {
}

func (as *AS) Test1() {}

func exercise01() {
	var as AS = AS{}
	// 必须加&才不报错
	var a A = &as
	fmt.Println(a)
}

type Hero struct {
	Name string
	Age  int
}

func (h Hero) String() string {
	return fmt.Sprintf("%s 今年%d岁了", h.Name, h.Age)
}

type HeroSlice []Hero

func (hs HeroSlice) Len() int {
	return len(hs)
}

// Less 就是决定排序的标准
// 按Hero的年龄从小到大
func (hs HeroSlice) Less(i, j int) bool {
	return hs[i].Age < hs[j].Age
}

func (hs HeroSlice) Swap(i, j int) {
	hs[i], hs[j] = hs[j], hs[i]
}

func (hs HeroSlice) Print() {
	for _, i := range hs {
		fmt.Println(i)
	}
}

func exercise02() {
	// 实现对Hero结构体切片的排序： sort.Sort(data Interface)
	var hs HeroSlice = make(HeroSlice, 10)
	for i := 0; i < 10; i++ {
		var no = rand.Intn(100)
		hs[i] = Hero{fmt.Sprintf("Hero%d", no), no * 20}
	}
	hs.Print()
	fmt.Println("-----------------------------------------")
	sort.Sort(hs)
	fmt.Println("-----------------------------------------")
	hs.Print()
}

func main() {
	exercise02()
}
